<template>
  <div class="page-container">
    <ac-table
      ref="dataTable"
      pkName="id"
      dataUrl="/flowOpinion/queryPage"
      :filters="filters"
      :filterFields="filterFields"
      :columns="columns"
      @handleTableOperator="handleTableOperator"
      @handleCellOperator="handleCellOperator"
      :checkRowOpShow="checkRowOpShow"
    >
    </ac-table>

    <el-drawer
      size="500px"
      v-if="dialogVisible"
      :append-to-body="true"
      :title="dialogTitle"
      :visible="dialogVisible"
      :close-on-click-modal="false"
      :wrapperClosable="!this.dialogEdit"
    >
      <template #title>
        <div role="heading" tabindex="0">
          <div class="el-drawer-title">
            <span>{{dialogTitle}}</span>
            <span>
              <button
                class="back-button"
                @click="dialogVisible=false">返回上一级
              </button>
            </span>
          </div>
        </div>
      </template>
      <el-form
        ref="dataForm"
        label-width="80px"
        label-position="right"
        :model="formData"
        :disabled="!this.dialogEdit"
        :rules="rules"
        :hide-required-asterisk="!this.dialogEdit"
        :size="size"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="意见" prop="opinion">
              <el-input v-model="formData.opinion" type="textarea" :rows="10"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!this.dialogEdit">
          <el-col :span="24">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="formData.createTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="el-drawer__footer">
        <el-button
          v-if="this.dialogEdit"
          :size="size"
          @click="dialogVisible=false"
        >取消
        </el-button>
        <el-button
          v-if="this.dialogEdit"
          :size="size" type="primary"
          @click="submitForm"
          :loading="editLoading"
        >提交
        </el-button>
        <el-button
          v-if="!this.dialogEdit"
          :size="size"
          @click="dialogVisible=false"
        >关闭
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedValue: '',
      size: 'small',

      filters: {
        name: ''
      },
      // 分页查询条件
      filterFields: [
        {prop: "opinion", label: "意见"}
      ],
      // 分页列表字段
      columns: [
        {prop: "opinion", label: "意见", minWidth: 120, click: true},
        {prop: "createTime", label: "创建时间", width: 160},
      ],
      // 表单数据验证规则
      rules: {
        opinion: [
          {required: true, message: '请输入意见', trigger: 'blur'},
          {max: 4000, message: '长度必须小于等于 4000 个字符！', trigger: 'blur'}
        ],
      },
      formData: {}, // 弹窗表单数据

      tableDialogTitle: '意见编辑', // 新增/编辑/查看弹窗标题
      tableDialogVisible: false, // 弹窗是否显示
      tableDialogEdit: true, // 弹窗表单是否可编辑

      dialogTitle: '', // 新增/编辑/查看弹窗标题
      dialogVisible: false, // 弹窗是否显示
      dialogEdit: true, // 弹窗表单是否可编辑
      editLoading: false, // 弹窗表单提交状态
      user: {},
    }
  },
  methods: {
    checkRowOpShow(op, row) {
      return row.createId === this.user.id;
    },
    // 单元格操作
    handleCellOperator: function(property, value, row) {
      this.formData = Object.assign({}, row);
      this.dialogVisible = true;
      this.dialogEdit = false;
      this.dialogTitle = "查看";

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate();
      });
    },
    handleTableOperator: function(button, row) {
      this.dialogVisible = true;
      this.dialogEdit = true;
      this.dialogTitle = button.buttonName;
      if (button.buttonType === 'edit') {
        this.formData = Object.assign({}, row, {button: button});
      } else {
        this.formData = {button: button};
      }

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate();
      });
    },
    // 表单提交
    submitForm: function() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm('请确认是否提交？', '提示', {type: 'info'}).then(() => {
            this.editLoading = true;
            this.$api.post('/flowOpinion/save', this.formData).then(res => {
              if(res.code === '1') {
                this.$message.success(res.message);
                this.dialogVisible = false;
                this.$refs.dataTable.findPage();
              } else {
                this.$message.error(res.message);
              }
              this.editLoading = false;
            }).catch(() => {
              this.editLoading = false;
            })
          });
        } else {
          this.$message.error('数据录入不正确，请重新检查！');
        }
      })
    }
  },
  created() {
    this.user = this.$userUtil.getUser();
  }
}
</script>

<style scoped>
</style>